/*
 * ManagerFrame.java
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import model.Job;
import model.ListManager;
import model.Park;

/**
 * ManagerFrame is a JFrame that displays all
 * Job information for the Parks for the current user.
 * 
 * @author Sean Brown
 * @author Carlo Consolacion
 * @version 2.0
 */
@SuppressWarnings("serial")
public class ManagerFrame extends JFrame {
	
	/**
	 * Default window size.
	 */
	private static final Dimension WINDOW_SIZE = new Dimension(800, 600);
	
	/**
	 * The ListManager for this run of the program.
	 */
	private final ListManager my_lm;
	
	/**
	 * The menu bar for this JFrame.
	 */
	private final ManagerMenuBar my_mmb;
	
	/**
	 * The WelcomeFrame that opened this frame.
	 */
	private final WelcomeFrame my_welcome;
	
	/**
	 * The JPanel that holds all of the job info for
	 * the parks managed by this user.
	 */
	private final JPanel my_jobs_panel;
	
	/**
	 * The Constructor which instantiates all fields.
	 * 
	 * @param the_welcome The WelcomeFrame that opened this window.
	 * @param the_lm The ListManager that is associated with this run.
	 */
	public ManagerFrame(final WelcomeFrame the_welcome, final ListManager the_lm) {
		super("Urban Parks: Park Manager");
		my_lm = the_lm;
		my_welcome = the_welcome;
		my_mmb = new ManagerMenuBar(the_welcome, this, my_lm);
		my_jobs_panel = new JPanel();
	}
	
	/**
	 * Sets up all panels, preferences, and shows the frame
	 * on screen.
	 */
	public void start() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		 
		//create custom close operation
		addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
			exit();
		}});

		UIManager.put("Panel.background", Color.WHITE);
		setJMenuBar(my_mmb);
		final JPanel my_main_layout = new JPanel(new BorderLayout());
		
		my_main_layout.add(welcomePanel(), BorderLayout.NORTH);
		drawJobListPanel();
		JScrollPane scroller = new JScrollPane
				(my_jobs_panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
						JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		my_main_layout.add(scroller, BorderLayout.CENTER);
		
		add(my_main_layout);
		setPreferredSize(WINDOW_SIZE);
		pack();
		setLocationRelativeTo(getRootPane());
		setVisible(true);
	}
	
	public void exit() {
		my_welcome.exitProcedure(this);
	}

	/**
	 * The job list of the Park Manager's parks screen.
	 * 
	 * @return
	 */
	private void drawJobListPanel() {
		my_jobs_panel.removeAll();
		final List<Park> my_parks = my_lm.getParksManaged();
		final JPanel grid = new JPanel();
		grid.setLayout(new GridLayout(1, my_parks.size(), 5, 5));
		
		for (Park p : my_parks) {
			final JPanel park_listing = new JPanel();
			park_listing.setLayout(new BoxLayout(park_listing, BoxLayout.PAGE_AXIS));
			final JTextArea park_name = new JTextArea(p.getName());
			park_name.setFont(GUIUtil.largerFont(park_name));
			park_name.setEditable(false);
			park_listing.add(park_name);
			
			for (Job j : my_lm.getParkJobs(p.getID())) {
				final JTextArea job_type = 
						new JTextArea("Job Type: " + j.getName());
				job_type.setEditable(false);
				job_type.setFont(new Font(job_type.getFont().getName(), 
						Font.BOLD, job_type.getFont().getSize()));
				final JTextArea start_end = new JTextArea();
				start_end.setEditable(false);
				start_end.append("Start\n" + 
						Job.dateToString(j.getStartTime()));
				start_end.append(", " + 
						Job.timeToString(j.getStartTime()));
				start_end.append("\nEnd\n" + 
						Job.dateToString(j.getEndTime()));
				start_end.append(", " + 
						Job.timeToString(j.getEndTime()));
				final JTextArea vol_label = new JTextArea("Volunteers:");
				vol_label.setEditable(false);
				vol_label.setFont(new Font(vol_label.getFont().getName(), 
						Font.BOLD, vol_label.getFont().getSize()));
				final JTextArea vols = new JTextArea(j.getSlotsToString());
				vols.setEditable(false);
				park_listing.add(job_type);
				park_listing.add(start_end);
				park_listing.add(vol_label);
				park_listing.add(vols);
				park_listing.add(new JLabel(" ")); //spacer
			}
			final JPanel squeeze = new JPanel(new FlowLayout());
			squeeze.add(park_listing);
			grid.add(squeeze);
		}
		my_jobs_panel.setLayout(new FlowLayout());
		my_jobs_panel.add(grid);
	}
	
	/**
	 * Returns a JPanel that shows a welcome message to the 
	 * current user.
	 * 
	 * @return JPanel welcome to display.
	 */
	private JPanel welcomePanel() {
		JPanel result = new JPanel();
		JLabel user_label = new JLabel("Welcome " + my_lm.getCurrentUser().getName() + "!", JLabel.LEFT);
		user_label .setFont(GUIUtil.largerFont(result));
		user_label .setHorizontalAlignment(JLabel.LEFT);					
		result.add(user_label);
		return result;
	}

	/**
	 * Method to call when a job is added.
	 */
	public void jobAdded() {
		drawJobListPanel();
		validate();
	}

}
